package com.framework.loippi.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.Notice;
import com.framework.loippi.entity.NoticeToUser;
import com.framework.loippi.service.NoticeService;
import com.framework.loippi.service.NoticeToUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName NoticeController
 * @Description TODO
 * @Author fsociety
 * @Date 9/1/2020 下午5:31
 * @Version 1.0
 */
@Controller("apiNoticeController")
@RequestMapping("/api/notice")
@Api(tags = {"通知信息"})
public class NoticeController extends ApiBaseController {

    @Autowired
    private NoticeToUserService noticeUserService;

    @RequestMapping(value = "/findByUserId.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("根据用户id查看通知")
    public @ResponseBody
    String findByUserId (Long userId) {
        List<NoticeToUser> userNotices = noticeUserService.findByUserId(userId);

        return ApiUtils.success(userNotices);
    }

    @RequestMapping(value="/setStatus.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("设置通知状态")
    public @ResponseBody
    String setNoticeStatus(Long noticeId) {
        NoticeToUser noticeToUser = noticeUserService.find(noticeId);
        noticeToUser.setNoticeStatus(1);
        noticeUserService.update(noticeToUser);
       return ApiUtils.success();
    }

    @RequestMapping(value = "/countUnreadNotice.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("查询用户未读的消息数量")
    public @ResponseBody
    String countUnreadNotice(Long userId) {
        Long total = noticeUserService.countUnreadNotice(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("total", total);

        return ApiUtils.success(map);
    }
}
